// *******************************************************************************************************
// # エリア
//   ・検索結果 リスト
// *******************************************************************************************************

var ResultBridge = ResultBridge || {};

var PageMove;

$(function() {
	var isUnderIe6 = uaInfo.isIe && uaInfo.ieVr <= 6,
		isUnderIe7 = uaInfo.isIe && uaInfo.ieVr <= 7;


	/*******************************************************
	 *　tooltip
	 *******************************************************/
	$('.tooltip').powerTip({
		placement: isUnderIe6 ? 'e' : 'n'
	});
	$('.tooltip.zoom').each(function(i){
		$(this).data('powertiptarget', $(this).next().attr('id'));
	});


	/*******************************************************
	 * まとめてお問い合わせ
	 *******************************************************/
	var isSelected = false,
		SelectMax = 20;

	$('.mod_table.mod_table_col').find('tr').each(function(i){
		var $self = $(this);
		if (!isUnderIe7) {
			$self.find('input')
				.on('ifChecked', function(e){
					changeCheckBox();
				})
				.on('ifUnchecked', function(e){
					changeCheckBox();
				});
		} else {
			$self.find('input')
				.on('click', function(e){
					changeCheckBox();
				});
		};
	});

	function changeCheckBox(){
		var _isSlt = false;
		$('.mod_table.mod_table_col .mod_check').each(function(e){
			if ($(this).find('input').prop('checked')) {
				_isSlt = true;
				return false;
			};
		});
		if (isSelected != _isSlt) {
			isSelected = _isSlt;
			if (isSelected) {
				changeInquiryBtn(true);
				ResultBridge.appendShow();
			} else {
				changeInquiryBtn(false);
				ResultBridge.appendHide();
			}
		};
	};
	changeCheckBox();

	var $allBtnUdr = $('.mod_box_section.box_pager .button a'),
		$allBtnWrp = $('.mod_btn_append a');

	$allBtnUdr.on('click', function(e){
		var _n = checkInquiryIsOver();
		if (_n < 0) {
			e.preventDefault();
			if (_n == -1) {
				modalOverSelect.show();
			};
		};
	});
	$allBtnWrp.on('click', function(e){
		var _n = checkInquiryIsOver();
		if (_n < 0) {
			e.preventDefault();
			if (_n == -1) {
				modalOverSelect.show();
			};
		};
	});

	function changeInquiryBtn(b) {
		if (b) {
			$allBtnUdr.removeClass('mod_btn_disabled');
			$allBtnWrp.removeClass('mod_btn_disabled');
		} else {
			$allBtnUdr.addClass('mod_btn_disabled');
			$allBtnWrp.addClass('mod_btn_disabled');
		};
	};

	function checkInquiryIsOver(){
		if (isSelected) {
			var _num = 0;
			$('.mod_table.mod_table_col .mod_check').each(function(i){
				if ($(this).find('input').prop('checked')) {
					_num++;
				};
			});
			if (SelectMax < _num) return -1;
			else return 1;
		};
		return -2;
	};

	var modalOverSelect = new APMN.Modal({
		target: $('#modal_gomen'),
		close: $('#modal_gomen .modal_close'),
		ieVr: !(isUnderIe6)
	});

	function submit(action, method, values) {
 	   var form = $('<form/>', {
	        action: action,
	        method: method
	    });
	    $.each(values, function() {
	        form.append($('<input/>', {
	            type: 'hidden',
	            name: this.name,
	            value: this.value
	        }));    
	    });
	    form.appendTo('body').submit();
	}
	
	function search(page) {
		var items=[];
		var typ = $('input[name="search_type"]').attr("value");
		items.push({name:'search_type',value:typ});

		$('.search_target').each(function(index){
			items.push({name:'target_'+index,
				    value:$(this).attr("id")});
		});

		$('.mod_checkbox input:checked').each(function(index){
			var elem = $(this);
			items.push({name:elem.attr("name"),
				    value:elem.attr("value"),});
		});

		$('select').each(function(index){
			var elem = $(this);
			items.push({name:elem.attr("name"),
				    value:elem.find("option:selected").attr("value"),});
		});

		items.push({name:'page',value:page||1});
		submit('/rstobi/rstobi','POST',items);
	}
	
	function go(page) {
		var items=[];
		var typ = $('input[name="search_type"]').attr("value");
		items.push({name:'search_type',value:typ});

		$('.search_target').each(function(index){
			items.push({name:'target_'+index,
				    value:$(this).attr("id")});
		});

		$('.mod_checkbox input:checked').each(function(index){
			var elem = $(this);
			items.push({name:elem.attr("name"),
				    value:elem.attr("value"),});
		});

		$('select').each(function(index){
			var elem = $(this);
			items.push({name:elem.attr("name"),
				    value:elem.find("option:selected").attr("value"),});
		});

		items.push({name:'page',value:page||1});
		submit('/rstobi/rstobi','POST',items);
	}
	PageMove = go;
});